import {
  addLogisticsInfoServer,
  appConfigServer,
  checkCodeByDigitWalletService,
  checkWalletBindService,
  customerServiceMarkServer,
  customerServicePreviewServer,
  deliveryTimeServer,
  getAdvertServices,
  getAreaListServices,
  getCardByIdService,
  getCardListByUserIdService,
  getCustomePageConfigServices,
  getCustomerServiceAdjustServer,
  getCustomerServiceArticleServer,
  getCustomerServiceListServer,
  getCustomerServiceSearchServer,
  getLocalOrgDetailServices,
  getLocationByIPServices,
  getLogisticsCompanyServer,
  getMerchantActivityAddressServer,
  getMyPointServer,
  getNavListServices,
  getOrgInfoWithLocationServices,
  getSearchResultServer,
  getSecondIndustryServices,
  getSelfTakeService,
  getShopIndustryAdjustServices,
  getUserDetailServer,
  getWalletBindCodeService,
  getWalletBindService,
  modifyLogisticsInfoServer,
  orgSearchProductListServer,
  pointConfigServer,
  selectDevicePage,
  selectFiveArticle,
  sendCodeByDigitWalletService,
  topOrgListService,
  checkTokenService,
  getAgreementInfoService,
  getAgreementVersionService,
} from '@/services/otherApi';
import { getActivityOrgListService } from '@/services/takeOut';
import Taro from '@tarojs/taro';
import utils from '@/utils/index';
import { dealPayloadParams } from './commonFunc';

export default {
  namespace: 'otherApi',
  state: {
    // 应用设置
    appConfig: undefined,

    // 积分规则
    pointConfig: undefined,

    // 店铺商品搜索
    orgSearchProductList: [],
    orgSearchProductListParams: {},
    orgSearchProductListPagination: {},
    orgSearchProductListFinished: false,

    // 新店日历店铺列表
    newShopOrgList: [],
    newShopOrgListParams: {},
    newShopOrgListPagination: {},
    newShopOrgListFinished: false,

    // 广告内容
    // xxxAdvertInfo   xxx为前缀
    // xxxAdvertList
    newsInfoList: [],
    newsPages: {},
    vemInfoList: [],
    vemPages: {},
    teamAllAddress: [],
    userDetail: {},
    areaList: [],
    shopListClassifyParams: {},
    advert: [],
    advertInfo: {},
    advertList: [],
    shopBaseInfo: {},
    //首页本地生活商户列表
    localOrgParams: {},
    localOrgList: [],
    localOrgFinished: false,
    //自提点列表
    selfTakeParams: {},
    selfTakeList: [],
    selfTakeFinished: false,
    //首页功能导航区
    navList: [],
    //一级行业下有店铺的二级行业
    secondIndustryList: [],
    //根据IP获取的用户位置信息
    locationByIP: {},
    //店铺基本信息，带坐标距离
    orgInfoLocation: {},
    //商城金刚区行业
    shopIndustryAdjust: [],
    //搜索结果
    searchResultParams: {},
    searchResultList: [],
    searchResultPagination: {},
    searchResultFinished: false,
    searchResultLoading: false,
    //发送绑定数字钱包验证码
    bindCodeSend: false,
    digitWallet: {},
    digitWalletBindSatus: {},
    sendCodeResult: false,
    checkCodeResult: {}, //数字钱包支付验证码校验结果

    logisticsCompanyList: [],
    logisticsCompanySelected: {},

    customerServiceList: [],
    customerServiceArticle: {},
    customerServiceAdjust: [],

    customePageConfig: {},
    customePageName: '',
    customPageConfigPages: {},

    tabsPageName: 'homePage',

    appName: '',

    agreementInfoAccess: {}, //访问协议
    agreementInfoLogin: {}, //登录协议
  },
  effects: {
    // 应用设置
    *getAppConfig({ payload }, { call, put }) {
      yield put({
        type: 'setAppConfig',
        payload: undefined,
      });

      const res = yield call(appConfigServer, payload);
      if (res.code !== '1') {
        Taro.showToast({
          title: res.message,
          duration: 1000,
          icon: 'none',
        });
        return Promise.reject();
      }
      // Taro.setStorageSync('merchantId',res.content.merchantId)
      // Taro.eventCenter.trigger('changeMerchant')
      yield put({
        type: 'setAppConfig',
        payload: res.content,
      });

      return res.content;
    },

    // 积分规则
    *getPointConfig({ payload }, { call, put }) {
      yield put({
        type: 'setPointConfig',
        payload: undefined,
      });

      const res = yield call(pointConfigServer, payload);
      if (res.code !== '1') {
        Taro.showToast({
          title: res.message,
          duration: 1000,
          icon: 'none',
        });
        return Promise.reject();
      }

      yield put({
        type: 'setPointConfig',
        payload: res.content,
      });

      return res.content;
    },

    // 店铺商品搜索
    *fetchOrgSearchProductList({ payload }, { select, call, put }) {
      const params = yield select(({ otherApi }) => otherApi.orgSearchProductListParams);
      const curIsFinished = yield select(({ otherApi }) => otherApi.orgSearchProductListFinished);

      const isAlreadyFinished = dealPayloadParams(payload, params, curIsFinished);
      if (isAlreadyFinished) {
        return;
      }

      if (payload.page === 1) {
        yield put({
          type: 'setOrgSearchProductList',
          payload: {
            orgSearchProductList: [],
            orgSearchProductListFinished: false,
          },
        });
      }

      const res = yield call(orgSearchProductListServer, payload);
      if (res.code !== '1') {
        Taro.showToast({
          title: res.message,
          duration: 1000,
          icon: 'none',
        });
        return Promise.reject();
      }

      const { list, pagination } = res.content;
      let dataList = [];
      if (payload.page != 1) {
        const oldList = yield select(({ otherApi }) => otherApi.orgSearchProductList);
        dataList = [...oldList, ...list];
      } else {
        dataList = [...list];
      }
      let isFinished = false;
      if (list.length < payload.pageSize) {
        isFinished = true;
      }
      yield put({
        type: 'setOrgSearchProductList',
        payload: {
          orgSearchProductList: dataList,
          orgSearchProductListPagination: { ...pagination },
          orgSearchProductListParams: { ...payload },
          orgSearchProductListFinished: isFinished,
        },
      });
    },

    // 新店日历店铺列表
    *fetchNewShopOrgList({ payload }, { select, call, put }) {
      const params = yield select(({ otherApi }) => otherApi.newShopOrgListParams);
      const curIsFinished = yield select(({ otherApi }) => otherApi.newShopOrgListFinished);

      const isAlreadyFinished = dealPayloadParams(payload, params, curIsFinished);
      if (isAlreadyFinished) {
        return;
      }

      const res = yield call(getActivityOrgListService, payload);
      if (res.code !== '1') {
        Taro.showToast({
          title: res.message,
          duration: 1000,
          icon: 'none',
        });
        return Promise.reject();
      }

      const { list, pagination } = res.content;
      let dataList = [];
      if (payload.page != 1) {
        const oldList = yield select(({ otherApi }) => otherApi.newShopOrgList);
        dataList = [...oldList, ...list];
      } else {
        dataList = [...list];
      }
      let isFinished = false;
      if (dataList.length >= pagination.totalCount) {
        isFinished = true;
      }
      yield put({
        type: 'setNewShopOrgList',
        payload: {
          newShopOrgList: dataList,
          newShopOrgListPagination: { ...pagination },
          newShopOrgListParams: { ...payload },
          newShopOrgListFinished: isFinished,
        },
      });
    },

    *selectFiveArticle({ payload }, { call, put }) {
      const res = yield call(selectFiveArticle, payload);
      if (res.code === '1') {
        yield put({
          type: 'newsList',
          payload: {
            newsInfoList: res.content.list,
            newsPages: res.content.pagination,
          },
        });
      }
    },
    *selectDevicePage({ payload }, { call, put }) {
      const res = yield call(selectDevicePage, payload);
      if (res.code === '1') {
        yield put({
          type: 'vemList',
          payload: {
            vemInfoList: res.content.list,
            vemPages: res.content.pagination,
          },
        });
      }
    },

    *getAreaListModel({ payload }, { call, put }) {
      const res = yield call(getAreaListServices, payload);
      if (res.code === '1') {
        let { districtList } = res.content;
        let china = [];
        for (let i = 0; i < districtList.length; i++) {
          if (districtList[i].name == '中华人民共和国') {
            china = districtList[i].childList;
          }
        }
        yield put({
          type: 'getAreaList',
          payload: {
            areaList: china,
          },
        });
      }
    },

    //卡列表信息
    *getCardListByUserIdModel({ payload }, { call }) {
      const res = yield call(getCardListByUserIdService, payload);
      if (res.code === '1') {
        return res.content;
      }
    },

    //卡二维码字串
    *getCardByIdModel({ payload }, { call }) {
      const res = yield call(getCardByIdService, payload);
      if (res.code === '1') {
        return res.content;
      }
    },

    //我的积分
    *getMyPointModel({ payload }, { call }) {
      const res = yield call(getMyPointServer, payload);
      if (res.code === '1') {
        return res.content;
      }
    },
    // 活动配送时间段
    *getDeliveryTime({ payload }, { call }) {
      const res = yield call(deliveryTimeServer, payload);
      const { code, content = {} } = res;
      if (code === '1') {
        return content;
      } else {
        return Promise.reject();
      }
    },
    // 团餐商户地址查询
    *getTeamAllAddress({ payload }, { call, put }) {
      const res = yield call(getMerchantActivityAddressServer, payload);
      const { code, content = {} } = res;
      if (code === '1') {
        const list = content.addressInfoList?.map((item) => {
          return {
            ...item,
            district: undefined,
            mobile: content.mobile,
            contactName: content.userName,
          };
        });

        yield put({
          type: 'setTeamAllAddress',
          payload: list,
        });

        yield put({
          type: 'setTeamDefaultAddress',
          payload: list?.[0],
        });
      }
    },

    //查询用户卡券分基本信息
    *getUserDetailModel({}, { call, put }) {
      const res = yield call(getUserDetailServer);
      if (res.code === '1') {
        yield put({
          type: 'setUserDetail',
          payload: { ...res.content },
        });
      }
    },

    *getAdvertModel({ payload }, { call, put }) {
      const res = yield call(getAdvertServices, payload);
      if (res.code === '1') {
        let { advertisingPosition, advertisesList } = res.content;
        yield put({
          type: 'setAdvert',
          payload: {
            alias: payload.alias,
            advertInfo: advertisingPosition ?? undefined,
            advertList: advertisesList ?? [],
          },
        });
      } else {
        Taro.showToast({
          title: '获取广告位数据失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    *getAdvert({ payload }, { call, put }) {
      const res = yield call(getAdvertServices, payload);

      if (!payload.prefix) {
        throw new Error('need prefix');
      }

      if (res.code === '1') {
        let { advertisingPosition, advertisesList } = res.content;
        yield put({
          type: 'setPrefixAdvert',
          payload: {
            [payload.prefix + 'AdvertInfo']: advertisingPosition ?? undefined,
            [payload.prefix + 'AdvertList']: advertisesList ?? [],
          },
        });
      } else {
        Taro.showToast({
          title: '获取广告位数据失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    //获取首页功能导航区
    *getNavListModel({ payload }, { call, put }) {
      const res = yield call(getNavListServices, payload);
      if (res.code === '1') {
        yield put({
          type: 'setNavList',
          payload: {
            navList: [...res.content],
          },
        });
      } else {
        Taro.showToast({
          title: '获取导航区数据失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    //获取一级行业下有店铺的二级行业
    *getSecondIndustryModel({ payload }, { call, put }) {
      const res = yield call(getSecondIndustryServices, payload);
      if (res.code === '1') {
        let secondIndustryList = [
          {
            firstIndustryId: payload.firstIndustryId,
            secondIndustryId: '0',
            secondIndustryName: '全部',
          },
          ...res.content,
        ];
        yield put({
          type: 'setSecondIndustryList',
          payload: {
            secondIndustryList: secondIndustryList.map((item) => {
              item.title = item.secondIndustryName;
              return item;
            }),
          },
        });
      } else {
        Taro.showToast({
          title: '获取二级行业数据失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    *getLocalOrgDetail({ payload }, { call, put }) {
      const res = yield call(getLocalOrgDetailServices, payload);
      if (res.code === '1') {
        let { applicationList } = res.content;
        let objShop = { support: false };
        let objTakeOut = { support: false };
        let objPay = { support: false };
        if (applicationList && applicationList.length > 0) {
          for (let i = 0; i < applicationList.length; i++) {
            if (applicationList[i].applicationAlias == 'shop') {
              objShop = { support: true, orgId: applicationList[i].orgId };
            } else if (applicationList[i].applicationAlias == 'takeway') {
              objTakeOut = { support: true, orgId: applicationList[i].orgId };
            } else if (applicationList[i].applicationAlias == 'payment') {
              objPay = { support: true, orgId: applicationList[i].orgId };
            }
          }
        }
        res.content.objShop = objShop;
        res.content.objTakeOut = objTakeOut;
        res.content.objPay = objPay;
        yield put({
          type: 'setLocalOrgDetail',
          payload: {
            shopBaseInfo: { ...res.content },
          },
        });
      } else {
        Taro.showToast({
          title: '获取商户信息失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    //首页本地生活店铺列表
    *getLocalOrgListModel({ payload }, { select, call, put }) {
      const params = yield select(({ otherApi }) => otherApi.localOrgParams);
      const curIsFinished = yield select(({ otherApi }) => otherApi.localOrgFinished);

      const isAlreadyFinished = dealPayloadParams(payload, params, curIsFinished);
      if (isAlreadyFinished) {
        return;
      }

      const res = yield call(topOrgListService, payload);
      if (res.code == 1) {
        const list = res.content || [];
        let dataList = [];
        if (payload.page != 1) {
          const curDataList = yield select(({ otherApi }) => otherApi.localOrgList);
          dataList = [...curDataList, ...list];
        } else {
          dataList = [...list];
        }
        let isFinished = false;
        if (list.length < payload.pageSize) {
          isFinished = true;
        }
        yield put({
          type: 'setLocalOrgList',
          payload: {
            localOrgParams: { ...payload },
            localOrgFinished: isFinished,
            localOrgList: dataList,
          },
        });
      } else {
        Taro.showToast({
          title: '获取本地生活商户数据失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    //自提点列表
    *getSelfTakeListModel({ payload }, { select, call, put }) {
      const params = yield select(({ otherApi }) => otherApi.selfTakeParams);
      const curIsFinished = yield select(({ otherApi }) => otherApi.selfTakeFinished);

      const isAlreadyFinished = dealPayloadParams(payload, params, curIsFinished);
      if (isAlreadyFinished) {
        return;
      }

      const res = yield call(getSelfTakeService, payload);
      if (res.code == 1) {
        const list = res.content.list || [];
        let dataList = [];
        if (payload.page != 1) {
          const curDataList = yield select(({ otherApi }) => otherApi.selfTakeList);
          dataList = [...curDataList, ...list];
        } else {
          dataList = [...list];
        }
        let isFinished = false;
        if (list.length < payload.pageSize) {
          isFinished = true;
        }
        yield put({
          type: 'setSelfTakeList',
          payload: {
            selfTakeParams: { ...payload },
            selfTakeFinished: isFinished,
            selfTakeList: dataList,
          },
        });
      } else {
        Taro.showToast({
          title: '获取自提点数据失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    *getLocationByIPModel({ payload }, { call, put }) {
      const res = yield call(getLocationByIPServices, payload);
      if (res.code === '1') {
        yield put({
          type: 'setLocationByIP',
          payload: {
            locationByIP: { ...res.content, timeStamp: new Date().getTime() },
          },
        });

        return { ...res.content, timeStamp: new Date().getTime() };
      } else {
        Taro.showToast({
          title: 'IP定位失败',
          duration: 1000,
          icon: 'none',
        });
        return {};
      }
    },

    *getOrgInfoWithLocationModel({ payload }, { call, put }) {
      const res = yield call(getOrgInfoWithLocationServices, payload);
      if (res.code === '1') {
        yield put({
          type: 'setOrgInfoLocation',
          payload: {
            orgInfoLocation: { ...res.content },
          },
        });
      } else {
        Taro.showToast({
          title: '获取商户信息失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    *getShopIndustryAdjustModel({ payload }, { call, put }) {
      const res = yield call(getShopIndustryAdjustServices, payload);
      if (res.code === '1') {
        yield put({
          type: 'setShopIndustryAdjust',
          payload: {
            shopIndustryAdjust: [...res.content],
          },
        });
      } else {
        Taro.showToast({
          title: '获取推荐行业失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },

    *getSearchResultModel({ payload }, { select, call, put }) {
      const params = yield select(({ otherApi }) => otherApi.searchResultParams);
      const curIsFinished = yield select(({ otherApi }) => otherApi.searchResultFinished);

      const isAlreadyFinished = dealPayloadParams(payload, params, curIsFinished);
      if (isAlreadyFinished) {
        return;
      }

      yield put({
        type: 'setSearchResult',
        payload: {
          searchResultLoading: true,
        },
      });

      const res = yield call(getSearchResultServer, payload);
      if (res.code == 1) {
        const { list, pagination } = res.content;
        let dataList = [];
        if (payload.page != 1) {
          const curSearchResultList = yield select(({ otherApi }) => otherApi.searchResultList);
          dataList = [...curSearchResultList, ...list];
        } else {
          dataList = [...list];
        }
        let isFinished = false;
        if (dataList.length >= pagination.totalCount) {
          isFinished = true;
        }
        yield put({
          type: 'setSearchResult',
          payload: {
            searchResultList: dataList,
            searchResultPagination: { ...pagination },
            searchResultParams: { ...payload },
            searchResultFinished: isFinished,
            searchResultLoading: false,
          },
        });
      } else {
        Taro.showToast({
          title: '搜索商品失败',
          duration: 1000,
          icon: 'none',
        });
        yield put({
          type: 'setSearchResult',
          payload: {
            searchResultLoading: false,
          },
        });
      }
    },

    *getWalletBindCode({ payload }, { call, put }) {
      const res = yield call(getWalletBindCodeService, payload);
      if (res.code === '1') {
        yield put({
          type: 'setBindCodeSend',
          payload: {
            bindCodeSend: true,
          },
        });
        Taro.showToast({
          title: '发送验证码成功',
          duration: 2000,
          icon: 'none',
        });
      } else {
        Taro.showToast({
          title: '发送验证码失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *getWalletBind({ payload }, { call, put }) {
      const res = yield call(getWalletBindService, payload);
      if (res.code === '1') {
        yield put({
          type: 'setBindResult',
          payload: {
            digitWallet: {
              ...res.content,
              message: res.message,
              success: res.success,
            },
          },
        });
      } else {
        yield put({
          type: 'setBindResult',
          payload: {
            digitWallet: {
              message: res.message,
              success: res.success,
            },
          },
        });
      }
      Taro.redirectTo({ url: '/pages/mainPages/bindWalletResult/index' });
    },

    *checkWalletBindStatus({ payload }, { call, put }) {
      const { forNext = true } = payload;
      const res = yield call(checkWalletBindService, payload);
      if (res.code === '1') {
        yield put({
          type: 'setDigitWalletBindSatus',
          payload: {
            digitWalletBindSatus: {
              ...res.content,
            },
          },
        });

        let { digitalWalletId = '' } = res.content;
        if (digitalWalletId.length > 0) {
          yield put({
            type: 'setBindResult',
            payload: {
              digitWallet: {
                message: '用户已经绑定电子钱包',
                success: false,
              },
            },
          });
          forNext && Taro.redirectTo({ url: '/pages/mainPages/bindWalletResult/index' });
        } else {
          forNext && Taro.redirectTo({ url: '/pages/mainPages/bindWalletCode/index' });
        }
      } else {
        Taro.showToast({
          title: '数字钱包签约检查失败',
          duration: 2000,
          icon: 'none',
        });
        forNext && Taro.redirectTo({ url: '/pages/mainPages/bindWalletCode/index' });
      }
    },

    *sendCodeByDigitWallet({ payload }, { call, put }) {
      const res = yield call(sendCodeByDigitWalletService, payload);
      if (res.code === '1') {
        yield put({
          type: 'setSendCodeResult',
          payload: true,
        });
      } else {
        Taro.showToast({
          title: '发送验证码失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *checkCodeByDigitWallet({ payload }, { call, put }) {
      const res = yield call(checkCodeByDigitWalletService, payload);
      if (res.code === '1') {
        yield put({
          type: 'setCheckCodeResult',
          payload: {
            result: true,
            cnt: res.content,
          },
        });
      } else {
        yield put({
          type: 'setCheckCodeResult',
          payload: {
            result: false,
            cnt: res.content?.timestamp || '-1',
          },
        });
        Taro.showToast({
          title: '验证码错误，请重新输入',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *getLogisticsCompanyModel({ payload }, { call, put }) {
      const res = yield call(getLogisticsCompanyServer, payload);
      if (res.code === '1') {
        yield put({
          type: 'setLogisticsCompanyList',
          payload: [...res.content],
        });
      } else {
        Taro.showToast({
          title: '获取物流公司信息失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *addLogisticsInfo({ payload }, { call }) {
      const res = yield call(addLogisticsInfoServer, payload);
      if (res.code === '1') {
        Taro.showToast({
          title: '提交物流信息成功',
          duration: 2000,
          icon: 'none',
        });
      } else {
        Taro.showToast({
          title: '提交物流信息失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *modifyLogisticsInfo({ payload }, { call }) {
      const res = yield call(modifyLogisticsInfoServer, payload);
      if (res.code === '1') {
        Taro.showToast({
          title: '修改物流信息成功',
          duration: 2000,
          icon: 'none',
        });
      } else {
        Taro.showToast({
          title: '修改物流信息失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *getCustomerServiceList({ payload }, { call, put }) {
      const res = yield call(getCustomerServiceListServer, payload);
      if (res.code === '1') {
        yield put({
          type: 'setCustomerServiceList',
          payload: [...res.content],
        });
      } else {
        Taro.showToast({
          title: '获取客服内容信息失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *customerServiceSearch({ payload }, { call, put }) {
      const res = yield call(getCustomerServiceSearchServer, payload);
      if (res.code === '1') {
        yield put({
          type: 'setCSsearchResultList',
          payload: [...res.content],
        });
      } else {
        Taro.showToast({
          title: '搜索失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *getCustomerServiceAdjust({ payload }, { call, put }) {
      const res = yield call(getCustomerServiceAdjustServer, payload);
      if (res.code === '1') {
        yield put({
          type: 'setCustomerServiceAdjust',
          payload: [...res.content],
        });
      } else {
        Taro.showToast({
          title: '获取相关推荐失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *getCustomerServiceArticle({ payload }, { call, put }) {
      const res = yield call(getCustomerServiceArticleServer, payload);
      if (res.code === '1') {
        yield put({
          type: 'setCustomerServiceArticle',
          payload: { ...res.content },
        });
      } else {
        Taro.showToast({
          title: '获取文章详情失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },

    *customerServiceMark({ payload }, { call }) {
      const res = yield call(customerServiceMarkServer, payload);
      if (res.code === '1') {
      } else {
        Taro.showToast({
          title: '提交文章帮助性失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },
    *getCustomePreviewPageConfig({ payload }, { call, put }) {
      const res = yield call(customerServicePreviewServer, payload);
      if (Object.keys(res).length > 0) {
        let jsonData = {};
        try {
          jsonData = JSON.parse(res.content.reviewContent);
        } catch (e) {}
        yield put({
          type: 'setCustomePageConfig',
          payload: {
            customePageConfig: { ...jsonData },
            customePageName: payload.pageName,
          },
        });
      } else {
        Taro.showToast({
          title: '获取页面配置失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },
    *getCustomePageConfig({ payload }, { call, put }) {
      const res = yield call(getCustomePageConfigServices, payload);
      if (Object.keys(res).length > 0) {
        yield put({
          type: 'setCustomePageConfig',
          payload: {
            customePageConfig: { ...res },
            customePageName: payload.pageName,
          },
        });
      } else {
        Taro.showToast({
          title: '获取页面配置失败',
          duration: 2000,
          icon: 'none',
        });
      }
    },
    *checkTokenModel({ payload }, { call }) {
      const res = yield call(checkTokenService, payload);
      if (res.code === '1') {
        return res.content;
      } else {
        Taro.showToast({
          title: '校验token失败',
          duration: 1000,
          icon: 'none',
        });
      }
    },
    *getAgreementInfoModel({ payload }, { call, put }) {
      const res = yield call(getAgreementInfoService, payload);
      if (res.code === '1') {
        res.content?.content?.length > 0 && (res.content.content = JSON.parse(res.content.content));
        if (payload.type == 1) {
          yield put({
            type: 'setAgreementInfoAccess',
            payload: { ...res.content },
          });
        } else if (payload.type == 2) {
          yield put({
            type: 'setAgreementInfoLogin',
            payload: {
              ...res.content,
              isSelected: res.content?.selectType == 1 ? true : false,
            },
          });
        }
      } else {
        Taro.showToast({
          title: '获取协议失败',
          duration: 1500,
          icon: 'none',
        });
      }
    },
    *getAgreementInfoVersionModel({ payload }, { call, put }) {
      const res = yield call(getAgreementVersionService, payload);
      if (res.code === '1') {
        if (payload.type == 1) {
          let curAgreementVersion = utils.getLocalStorage('agreementAccess');
          let isNeedShow = false;
          if (
            res?.content?.existed != curAgreementVersion?.existed ||
            curAgreementVersion?.version != res?.content?.version
          ) {
            utils.setLocalStorage('agreementAccess', res.content);
            if (res?.content?.existed) {
              isNeedShow = true;
            }
          }
          yield put({
            type: 'setAgreementAccessIsShow',
            payload: {
              agreementAccessIsShow: isNeedShow,
            },
          });
          yield put({
            type: 'setAgreementVersionAccess',
            payload: { ...res.content },
          });
        } else if (payload.type == 2) {
          yield put({
            type: 'setAgreementVersionLogin',
            payload: { ...res.content },
          });
        }
      } else {
        Taro.showToast({
          title: '获取协议版本失败',
          duration: 1500,
          icon: 'none',
        });
      }
    },
  },
  reducers: {
    // 应用设置
    setAppConfig(state, { payload }) {
      let { iconUrl, name } = payload ?? {};

      if (!iconUrl) {
        iconUrl = 'https://ht-fine-static.oss-cn-beijing.aliyuncs.com/weapp/aNeedUp/logo.png';
      }

      if (!name) {
        name = '';
      }
      Taro.setStorageSync('appConfig',payload)
      return {
        ...state,
        appConfig: { iconUrl, name },
      };
    },

    // 积分规则
    setPointConfig(state, { payload }) {
      return {
        ...state,
        pointConfig: payload,
      };
    },

    // 店铺商品搜索
    setOrgSearchProductList(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 新店日历店铺列表
    setNewShopOrgList(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    newsList(state, { payload }) {
      const { newsInfoList, newsPages } = payload;
      return {
        ...state,
        newsInfoList,
        newsPages,
      };
    },
    vemList(state, { payload }) {
      const { vemInfoList, vemPages } = payload;
      return {
        ...state,
        vemInfoList,
        vemPages,
      };
    },
    getAreaList(state, { payload }) {
      const { areaList } = payload;
      return {
        ...state,
        areaList,
      };
    },

    resetTeamAddress(state, {}) {
      return {
        ...state,
        teamAllAddress: [],
        teamDefaultAddress: undefined,
      };
    },
    setTeamAllAddress(state, { payload }) {
      return {
        ...state,
        teamAllAddress: payload,
      };
    },
    // 团餐默认地址
    setTeamDefaultAddress(state, { payload }) {
      return {
        ...state,
        teamDefaultAddress: payload,
      };
    },
    setUserDetail(state, { payload }) {
      return {
        ...state,
        userDetail: payload,
      };
    },
    setShopListClassifyParams(state, { payload }) {
      return {
        ...state,
        shopListClassifyParams: payload,
      };
    },
    setAdvert(state, { payload }) {
      const { alias, advertInfo, advertList } = payload;
      let tmpAdvert = { ...state.advert };
      tmpAdvert[alias] = { advertInfo, advertList };
      return {
        ...state,
        advert: tmpAdvert,
      };
    },
    setPrefixAdvert(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setLocalOrgDetail(state, { payload }) {
      const { shopBaseInfo } = payload;
      return {
        ...state,
        shopBaseInfo,
      };
    },
    setLocalOrgList(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setSelfTakeList(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setNavList(state, { payload }) {
      const { navList } = payload;
      return {
        ...state,
        navList,
      };
    },
    setSecondIndustryList(state, { payload }) {
      const { secondIndustryList } = payload;
      return {
        ...state,
        secondIndustryList,
      };
    },
    setLocationByIP(state, { payload }) {
      const { locationByIP } = payload;
      return {
        ...state,
        locationByIP,
      };
    },
    setOrgInfoLocation(state, { payload }) {
      const { orgInfoLocation } = payload;
      return {
        ...state,
        orgInfoLocation,
      };
    },
    setShopIndustryAdjust(state, { payload }) {
      const { shopIndustryAdjust } = payload;
      return {
        ...state,
        shopIndustryAdjust,
      };
    },
    setSearchResult(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setBindCodeSend(state, { payload }) {
      const { bindCodeSend } = payload;
      return {
        ...state,
        bindCodeSend,
      };
    },
    setBindResult(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setDigitWalletBindSatus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setSendCodeResult(state, { payload }) {
      return {
        ...state,
        sendCodeResult: payload,
      };
    },
    setCheckCodeResult(state, { payload }) {
      return {
        ...state,
        checkCodeResult: { ...payload },
      };
    },
    setLogisticsCompanyList(state, { payload }) {
      return {
        ...state,
        logisticsCompanyList: payload,
      };
    },
    setSelectedLogisticsCompany(state, { payload }) {
      return {
        ...state,
        logisticsCompanySelected: { ...payload },
      };
    },
    setCustomerServiceList(state, { payload }) {
      return {
        ...state,
        customerServiceList: payload,
      };
    },
    setCSsearchResultList(state, { payload }) {
      return {
        ...state,
        CSsearchResultList: payload,
      };
    },
    setCustomerServiceAdjust(state, { payload }) {
      return {
        ...state,
        customerServiceAdjust: payload,
      };
    },
    setCustomerServiceArticle(state, { payload }) {
      return {
        ...state,
        customerServiceArticle: payload,
      };
    },
    setCustomePageConfig(state, { payload }) {
      const { customePageConfig = {}, customePageName = '' } = payload;
      const { customPageConfigPages = {} } = state;
      customPageConfigPages[customePageName] = customePageConfig;
      return {
        ...state,
        customePageConfig,
        customePageName,
        customPageConfigPages,
      };
    },
    setTabsPageName(state, { payload }) {
      return {
        ...state,
        tabsPageName: payload,
      };
    },
    setAppNameForBankBind(state, { payload }) {
      return {
        ...state,
        appName: payload,
      };
    },
    setAgreementInfoAccess(state, { payload }) {
      return {
        ...state,
        agreementInfoAccess: payload,
      };
    },
    setAgreementVersionAccess(state, { payload }) {
      return {
        ...state,
        agreementVersionAcess: payload,
      };
    },
    setAgreementAccessIsShow(state, { payload }) {
      return {
        ...state,
        agreementAccessIsShow: payload.agreementAccessIsShow,
      };
    },
    setAgreementInfoLogin(state, { payload }) {
      return {
        ...state,
        agreementInfoLogin: payload,
      };
    },
    setAgreementVersionLogin(state, { payload }) {
      return {
        ...state,
        agreementVersionLogin: payload,
      };
    },
    setAgreementDetail(state, { payload }) {
      return {
        ...state,
        agreementDetail: payload,
      };
    },
  },
};
